<template>
    <div>
        <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeSelect:changeSelect,changeTotalSize:changeTotalSize,events:events}">
        </iview-table>
        <Modal v-model="loading">
          <h3 slot="header" style="color:#2D8CF0">正在打开病案,请稍后...</h3>
          <Progress :percent="this.$store.state.app.percent"></Progress>
          <div slot="footer">
          </div>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        name: 'text-editor',
        components: {
            iviewTable,
            iviewPage
        },
        data () {
            return {
                baseURL: '',
                taskURL: '/user/treatment/task',
                showAdd: false,
                loading: false,
                selectRow: {},
                selectRows: [],
                doctors: [],
                nurses: [],
                projects: [],
                equipments: [],
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treatment/task',
                    defaultFilters: [
                        {'field': 'status', 'value1': 2, 'operate': '<'}
                    ]
                },
                dataModel: {
                    menuId: 0,
                    columns: [
                        // {
                        //     type: 'selection',
                        //     width: 60,
                        //     align: 'center'
                        // },
                        {
                            title: '患者',
                            key: 'patientName',
                            sortable: true
                        },{
                            title: '病历号',
                            key: 'medicalNumber',
                            sortable: true
                        },{
                            title: '专家',
                            key: 'expertName',
                            sortable: true
                        },{
                            title: '医生',
                            key: 'doctorName',
                            sortable: true
                        },{
                            title: '护士',
                            key: 'nurseName',
                            sortable: true
                        },{
                            title: '治疗项目',
                            key: 'projectName',
                            sortable: true
                        },{
                            title: '治疗设备',
                            key: 'equipmentName',
                            sortable: true
                        },{
                            title: '治疗开始时间',
                            key: 'startTime',
                            sortable: true,
                            render: (h,params) => {
                                return h('span', this.$moment(params.row.startTime).format('YYYY-MM-DD HH:mm'))
                            }
                        },{
                            title: '治疗结束时间',
                            key: 'endTime',
                            sortable: true,
                            render: (h,params) => {
                                return h('span', this.$moment(params.row.endTime).format('YYYY-MM-DD HH:mm'))
                            }
                        },
                        // {
                        //     title: '备注',
                        //     key: 'remark',
                        //     sortable: true
                        // },
                        {
                            title: '状态',
                            key: 'status',
                            sortable: true,
                            render: (h, params) => {
                                let color = ''
                                let temp = params.row.status
                                if (temp === 0) {
                                    temp = '等待治疗'
                                    color = '#ff9900'
                                } else if (temp === 1) {
                                    temp = '治疗中'
                                    color = '#ed3f14'
                                }
                                return h('Tag',{
                                    props: {
                                        // type: 'border',
                                        color: color
                                    }
                                }, temp)
                            }
                        },
                        {
                            title: '操作',
                            width: 200,
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'info',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.admission(params.row)
                                            }
                                        }
                                    }, '接诊'),
                                    h('Button', {
                                        props: {
                                            type: 'info',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.complete(params.row)
                                            }
                                        }
                                    }, '完成')
                                ]);
                            }
                        }
                    ],
                    order: 'start_time asc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            };
        },
        filters: {
        },
        methods: {
            events (item, row) {
            },
            admission (row) {
                this.$store.state.app.percent = 0
                if (row.status === 0) {
                    let url = this.taskURL + '/admission'
                    this.$ajax.post(url, row).then(r => {
                        let {code, msg} = r.data
                        if (code === 0) {
                            this.toMedical(row.patientId)
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.toMedical(row.patientId)
                }
            },
            toMedical (patientId) {
                this.loading = true
                let timer = setInterval(() => {
                    let percent = this.$store.state.app.percent
                    if (percent < 100){
                        this.$store.state.app.percent = this.$store.state.app.percent + 20
                    }
                    if (percent >= 100) {
                        clearInterval(timer);
                        setTimeout(() => {
                            this.loading = false
                        },50)
                    }
                },50)
                setTimeout(() => {
                    let query = {
                        id: patientId
                    }
                    this.$store.state.app.patientId = patientId
                    this.$bus.toPath('/medical', query)
                },100)
            },
            complete (row) {
                let url = this.taskURL + '/complete'
                this.$ajax.post(url, row).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '操作成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            refresh () {
                this.queryModel.defaultFilters.length = 0
                this.queryModel.defaultFilters.push({'field': 'status', 'value1': 2, 'operate': '<'})
                var user = this.$store.state.app.loginUser
                if (user.isAdmin !== 1) {
                    if (user.isExpert === 1) {
                        this.queryModel.defaultFilters.push({'field': 'expert_id', 'value1': user.id, 'operate': '=='})
                    } else if (user.job === 1) {
                        this.queryModel.defaultFilters.push({'field': 'doctor_id', 'value1': user.id, 'operate': '=='})
                    }
                }
                this.$refs.table.refresh()
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            // this.init()
            // setInterval(this.)
        }
    }
</script>

<style>
</style>
